import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'gif_refresher.dart';

class HomeIndexVideoList extends StatefulWidget {
  HomeIndexVideoList({Key key, @required this.type}) : super(key: key);
  final String type;

  @override
  _HomeIndexVideoListState createState() => _HomeIndexVideoListState();
}

class _HomeIndexVideoListState extends State<HomeIndexVideoList>
    with AutomaticKeepAliveClientMixin {
  RefreshController _refreshController = RefreshController();
  var list = [];
  bool firstLoad = true;
  bool firstLoadFail = false;
  int index = 1;

  @override
  void initState() {
    super.initState();
    print('初始化状态，开始请求第一次数据');
    Future.delayed(Duration(seconds: 3), () {
      print('第一次数据接口结束');
      setState(() {
        firstLoad = false;
        index = 5;
        list.addAll(
          [
            {
              'title': '标题名称1',
              'author': '作者',
            },
            {
              'title': '标题名称2',
              'author': '作者',
            },
            {
              'title': '标题名称3',
              'author': '作者',
            },
            {
              'title': '标题名称4',
              'author': '作者',
            },
            {
              'title': '标题名称5',
              'author': '作者',
            },
          ],
        );
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return RefreshConfiguration(
      maxOverScrollExtent: 60,
      child: GifRefresher(
        controller: _refreshController,
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 1));
          _refreshController.refreshCompleted();
        },
        child: ListView(
          children: firstLoad
              ? [Center(child: Text('加载中...'))]
              : list.map((e) => VideoItem()).toList(),
        ),
        onLoading: () async {
          print(list.length);
          if (list.length >= 10) {
            _refreshController.loadNoData();
            return;
          }
          print('begin Load');
          // monitor network fetch
          await Future.delayed(Duration(milliseconds: 1000));
          print('after Load');
          setState(() {
            index++;
            list.add({
              'title': '标题名称' + index.toString(),
              'author': '作者',
            });
          });
          // if failed,use loadFailed(),if no data return,use LoadNodata()
          _refreshController.loadComplete();
        },
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}

class VideoItem extends StatelessWidget {
  const VideoItem({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        bottom: 10,
      ),
      decoration: BoxDecoration(color: Colors.grey[100]),
      child: Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(color: Colors.white),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Stack(
              children: [
                Image.asset(
                  'static/images/video_cover.jpg',
                  fit: BoxFit.fill,
                  width: MediaQuery.of(context).size.width - 20,
                ),
                Positioned.fill(
                  child: Center(
                      child: Icon(
                    Icons.play_circle_outline,
                    color: Colors.white24,
                    size: 60,
                  )),
                ),
                Positioned(
                  child: Text(
                    '10:23',
                    style: TextStyle(color: Colors.white24),
                  ),
                  bottom: 5,
                  right: 5,
                )
              ],
            ),
            Text('啦啦啦啦啦啦啦啦啦啦啦啦的视频名字哟啦啦啦啦啦啦啦啦啦啦啦啦的视频名字哟啦啦啦啦啦啦啦啦啦啦啦啦的视频名字哟',
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(fontSize: 18, letterSpacing: 2)),
          ],
        ),
      ),
    );
  }
}
